<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>赠送优惠券</title>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css"/>
</head>
<body>
    <div class="layui-row" style="margin-left: 100px">
        <div class="layui-col-md11">
            <table id="demo" lay-filter="test"></table>
        </div>
    </div>
    <div style="position: absolute; bottom: 40px; right:30px;">
        <button class="layui-btn" onclick="determine()">确定</button>
        <button class="layui-btn" onclick="cancel()">取消</button>
    </div>
</body>
    <script>
        var form;
        var $;
        var table;
        var layer;
        var upload;
        var laydate;
        var radioData;
        var arr = parent.getArr();
        layui.use(['laydate','form','jquery','table','layer','upload'],function() {
            form = layui.form;
            laydate = layui.laydate;
            $ = layui.jquery;
            table = layui.table;
            layer = layui.layer;
            upload = layui.upload;

            table.on('radio(test)', function(obj){
                radioData = obj.data; //选中行的相关数据
            });
        });

        $.ajax({
           url:'/UserList/queryCoupons',
           dataType:'json',
           success:function (data) {
               data = data.data;
               strTab(data);
           }
        });

        function strTab(data) {
            table.render({
                elem:'#demo',
                page:true,
                data:data,
                cols:[[
                    {type:'radio'},
                    {align:'center',title:'序号',width:60,type:'numbers'},
                    {align:'center',field:'couponCode',title:'优惠券编号'},
                    {align:'center',field:'couponName',title:'优惠券名称'},
                    {align:'center',title:'优惠券金额',field:'couponPrice'},
                    {align:'center',title:'开始时间',field:'startDate'},
                    {align:'center',title:'结束时间',field:'endDate'},
                    {align:'center',title:'发行数量',field:'issueQuantity'},
                ]]
            });
        }

        //赠送
        function determine() {
            if(radioData == null){
                layer.msg("请选择一种优惠券");
                return;
            }else if(arr.length > radioData.issueQuantity){
                layer.msg(radioData.couponName +"数量不足无法赠送");
                return;
            }else {
                $.ajax({
                    type:'post',
                    url:'/UserList/addUserCouponUsed',
                    data:{
                        "couponId":radioData.couponId,"obj":arr
                    },
                    dataType:'json',
                    traditional:true,
                    success:function (data) {
                        data = data.data;
                        if(data){
                            var layer = parent.layer;
                            layer.close(layer.index);
                            parent.layer.msg("赠送成功");
                        }else{
                            parent.layer.msg("赠送失败");
                        }
                    }
                });
            }
        }
        function cancel() {
            var layer = parent.layer;
            layer.close(layer.index);
        }
    </script>
</html>